package com.dragonwu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dragonwu.domain.Stock;
import com.dragonwu.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author DragonWu
 * @date 2022-10-02 18:28
 **/
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private StockMapper stockMapper;

    @Transactional
    @PostMapping("/add")
    public String add(@RequestBody Stock stock) {
        stockMapper.insert(stock);
        return "ok";
    }

    @Transactional
    @PutMapping("/update")
    public void update() {
        LambdaQueryWrapper<Stock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Stock::getOrderId, 123456L);
        Stock stock = stockMapper.selectOne(queryWrapper);
        stock.setNum(stock.getNum() - 1);
        stockMapper.updateById(stock);
    }
}
